package com.situ.allaia.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.allaia.dao.CartItemDao;
import com.situ.allaia.dao.OrderDao;
import com.situ.allaia.dao.OrderItemDao;
import com.situ.allaia.model.*;
import com.situ.allaia.service.OrderService;
import com.situ.allaia.util.PaginateInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;


@Service
public class OrderServiceImpl implements OrderService {
    private final OrderDao orderDao;
    private final CartItemDao cartItemDao;
    private final OrderItemDao orderItemDao;

    public OrderServiceImpl(OrderDao orderDao, CartItemDao cartItemDao, OrderItemDao orderItemDao) {
        this.orderDao = orderDao;
        this.cartItemDao = cartItemDao;
        this.orderItemDao = orderItemDao;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Order save(Order order) {
        //雪花算法
        Snowflake sf = IdUtil.getSnowflake(1, 1);
        order.setId(sf.nextId());
        order.setCreateTime(LocalDateTime.now());
        orderDao.insert(order);//保存订单

        //订单总金额
        BigDecimal total = new BigDecimal(0);
        //查询购物车项
        List<CartItem> cartItems = cartItemDao.findByUserId(order.getUser().getId());

        for (CartItem ci : cartItems) {
            OrderItem oi = new OrderItem();//创建订单项
            oi.setOrderId(order.getId());
            oi.setGood(ci.getGood());
            oi.setPrice(ci.getGood().getPrice());
            //缓存
            oi.setGoodName(ci.getGood().getName());
            oi.setGoodPic(ci.getGood().getPicUrl());
            oi.setAmount(ci.getAmount());
            //保存订单项
            orderItemDao.insert(oi);

            //统计所有订单项的总金额
            total = total.add(oi.getPrice().multiply(BigDecimal.valueOf(oi.getAmount())));
        }

        total = total.add(order.getShipping());
        //重新查出来
        Order db = orderDao.findById(order.getId());
        db.setSubTotal(total);//设置总金额
        //百分之五的税
        db.setTax(total.multiply(BigDecimal.valueOf(0.05)));
        orderDao.update(db);//对订单进行更新

        //删除所有购物车项
        cartItemDao.deleteByUserId(db.getUser().getId());

        return db;
    }

    @Override
    public int sendByIds(Collection<Long> ids) {
        int rows = 0;
        for (Long id : ids) {
            Order order = findById(id);
            if (order.getState().equals("已支付")) {
                order.setState("已发货");
                rows += patch(order) ? 1 : 0;
            }
        }
        return rows;
    }

    @Override
    public boolean patch(Order order) {
        return orderDao.patch(order) > 0;
    }

    @Override
    public List<Order> findByUserId(Integer userId) {
        return orderDao.findByUserId(userId);
    }

    @Override
    public Order findById(Long id) {
        return orderDao.findById(id);
    }

    @Override
    public List<Order> findAll(OrderSearchBean osb, PaginateInfo pi) {
        PageHelper.startPage(pi.getPageNo(), pi.getPageSize());

        List<Order> orders = orderDao.findAll(osb);

        PageInfo<Order> page = new PageInfo<>(orders);
        pi.setTotal(page.getTotal());

        return orders;
    }
}
